home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / tools / bison.lha / bison++-1.04 / conflicts.c < prev    next >
C/C++ Source or Header  |  1989-06-09  |  15KB  |  764 lines

  1. /* Find and resolve or report look-ahead conflicts for bison,
  2.    Copyright (C) 1984, 1989 Free Software Foundation, Inc.
  3.  
  4. This file is part of Bison, the GNU Compiler Compiler.
  5.  
  6. Bison is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 1, or (at your option)
  9. any later version.
  10.  
  11. Bison is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with Bison; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20.  
  21. #include <stdio.h>
  22. #include "machine.h"
  23. #include "new.h"
  24. #include "files.h"
  25. #include "gram.h"
  26. #include "state.h"
  27.  
  28.  
  29. #ifdef USG
  30. #include <memory.h>
  31. #define bcopy(src, dst, num) memcpy((dst), (src), (num))
  32. #endif
  33.  
  34. #ifdef __GNUC__
  35. #define alloca __builtin_alloca
  36. #else
  37. #ifdef sparc
  38. #include <alloca.h>
  39. #else
  40. extern char *alloca ();
  41. #endif
  42. #endif
  43.  
  44. extern char **tags;
  45. extern int tokensetsize;
  46. extern char *consistent;
  47. extern short *accessing_symbol;
  48. extern shifts **shift_table;
  49. extern unsigned *LA;
  50. extern short *LAruleno;
  51. extern short *lookaheads;
  52. extern int verboseflag;
  53.  
  54. void set_conflicts();
  55. void resolve_sr_conflict();
  56. void flush_shift();
  57. void log_resolution();
  58. void total_conflicts();
  59. void count_sr_conflicts();
  60. void count_rr_conflicts();
  61.  
  62. char any_conflicts;
  63. char *conflicts;
  64. errs **err_table;
  65. int expected_conflicts;
  66.  
  67.  
  68. static unsigned *shiftset;
  69. static unsigned *lookaheadset;
  70. static int src_total;
  71. static int rrc_total;
  72. static int src_count;
  73. static int rrc_count;
  74.  
  75.  
  76. void
  77. initialize_conflicts()
  78. {
  79.   register int i;
  80. /*  register errs *sp; JF unused */
  81.  
  82.   conflicts = NEW2(nstates, char);
  83.   shiftset = NEW2(tokensetsize, unsigned);
  84.   lookaheadset = NEW2(tokensetsize, unsigned);
  85.  
  86.   err_table = NEW2(nstates, errs *);
  87.  
  88.   any_conflicts = 0;
  89.  
  90.   for (i = 0; i < nstates; i++)
  91.     set_conflicts(i);
  92. }
  93.  
  94.  
  95. void
  96. set_conflicts(state)
  97. int state;
  98. {
  99.   register int i;
  100.   register int k;
  101.   register shifts *shiftp;
  102.   register unsigned *fp2;
  103.   register unsigned *fp3;
  104.   register unsigned *fp4;
  105.   register unsigned *fp1;
  106.   register int symbol;
  107.  
  108.   if (consistent[state]) return;
  109.  
  110.   for (i = 0; i < tokensetsize; i++)
  111.     lookaheadset[i] = 0;
  112.  
  113.   shiftp = shift_table[state];
  114.   if (shiftp)
  115.     {
  116.       k = shiftp->nshifts;
  117.       for (i = 0; i < k; i++)
  118.     {
  119.       symbol = accessing_symbol[shiftp->shifts[i]];
  120.       if (ISVAR(symbol)) break;
  121.       SETBIT(lookaheadset, symbol);
  122.     }
  123.     }
  124.  
  125.   k = lookaheads[state + 1];
  126.   fp4 = lookaheadset + tokensetsize;
  127.  
  128.   /* loop over all rules which require lookahead in this state */
  129.   /* first check for shift-reduce conflict, and try to resolve using precedence  */
  130.  
  131.   for (i = lookaheads[state]; i < k; i++)
  132.     if (rprec[LAruleno[i]])
  133.       {
  134.     fp1 = LA + i * tokensetsize;
  135.     fp2 = fp1;
  136.     fp3 = lookaheadset;
  137.   
  138.     while (fp3 < fp4)
  139.       {
  140.         if (*fp2++ & *fp3++)
  141.           {
  142.         resolve_sr_conflict(state, i);
  143.         break;
  144.           }
  145.       }
  146.       }
  147.  
  148.   /* loop over all rules which require lookahead in this state */
  149.   /* Check for conflicts not resolved above.  */
  150.  
  151.   for (i = lookaheads[state]; i < k; i++)
  152.     {
  153.       fp1 = LA + i * tokensetsize;
  154.       fp2 = fp1;
  155.       fp3 = lookaheadset;
  156.  
  157.       while (fp3 < fp4)
  158.     {
  159.       if (*fp2++ & *fp3++)
  160.         {
  161.           conflicts[state] = 1;
  162.           any_conflicts = 1;
  163.         }
  164.     }
  165.  
  166.       fp2 = fp1;
  167.       fp3 = lookaheadset;
  168.  
  169.       while (fp3 < fp4)
  170.     *fp3++ |= *fp2++;
  171.     }
  172. }
  173.  
  174.  
  175.  
  176. /* Attempt to resolve shift-reduce conflict for one rule
  177. by means of precedence declarations.
  178. It has already been checked that the rule has a precedence.
  179. A conflict is resolved by modifying the shift or reduce tables
  180. so that there is no longer a conflict.  */
  181.  
  182. void
  183. resolve_sr_conflict(state, lookaheadnum)
  184. int state;
  185. int lookaheadnum;
  186. {
  187.   register int i;
  188.   register int mask;
  189.   register unsigned *fp1;
  190.   register unsigned *fp2;
  191.   register int redprec;
  192.   errs *errp = (errs *) alloca (sizeof(errs) + ntokens * sizeof(short));
  193.   short *errtokens = errp->errs;
  194.  
  195.   /* find the rule to reduce by to get precedence of reduction  */
  196.   redprec = rprec[LAruleno[lookaheadnum]];
  197.  
  198.   mask = 1;
  199.   fp1 = LA + lookaheadnum * tokensetsize;
  200.   fp2 = lookaheadset;
  201.   for (i = 0; i < ntokens; i++)
  202.     {
  203.       if ((mask & *fp2 & *fp1) && sprec[i])
  204.     /* shift-reduce conflict occurs for token number i and it has a precision.
  205.        The precedence of shifting is that of token i.  */
  206.     {
  207.       if (sprec[i] < redprec)
  208.         {
  209.           if (verboseflag) log_resolution(state, lookaheadnum, i, "reduce");
  210.           *fp2 &= ~mask;  /* flush the shift for this token */
  211.           flush_shift(state, i);
  212.         }
  213.       else if (sprec[i] > redprec)
  214.         {
  215.           if (verboseflag) log_resolution(state, lookaheadnum, i, "shift");
  216.           *fp1 &= ~mask;  /* flush the reduce for this token */
  217.         }
  218.       else
  219.         {
  220.           /* Matching precedence levels.
  221.          For left association, keep only the reduction.
  222.          For right association, keep only the shift.
  223.          For nonassociation, keep neither.  */
  224.  
  225.           switch (sassoc[i])
  226.         {
  227.  
  228.         case RIGHT_ASSOC:
  229.               if (verboseflag) log_resolution(state, lookaheadnum, i, "shift");
  230.           break;
  231.  
  232.         case LEFT_ASSOC:
  233.               if (verboseflag) log_resolution(state, lookaheadnum, i, "reduce");
  234.           break;
  235.  
  236.         case NON_ASSOC:
  237.               if (verboseflag) log_resolution(state, lookaheadnum, i, "an error");
  238.           break;
  239.         }
  240.  
  241.           if (sassoc[i] != RIGHT_ASSOC)
  242.         {
  243.           *fp2 &= ~mask;  /* flush the shift for this token */
  244.           flush_shift(state, i);
  245.         }
  246.           if (sassoc[i] != LEFT_ASSOC)
  247.             {
  248.           *fp1 &= ~mask;  /* flush the reduce for this token */
  249.         }
  250.           if (sassoc[i] == NON_ASSOC)
  251.         {
  252.           /* Record an explicit error for this token.  */
  253.           *errtokens++ = i;
  254.         }
  255.         }
  256.     }
  257.  
  258.       mask <<= 1;
  259.       if (mask == 0)
  260.     {
  261.       mask = 1;
  262.       fp2++;  fp1++;
  263.     }
  264.     }
  265.   errp->nerrs = errtokens - errp->errs;
  266.   if (errp->nerrs)
  267.     {
  268.       /* Some tokens have been explicitly made errors.  Allocate
  269.      a permanent errs structure for this state, to record them.  */
  270.       i = (char *) errtokens - (char *) errp;
  271.       err_table[state] = (errs *) mallocate ((unsigned int)i);
  272.       bcopy (errp, err_table[state], i);
  273.     }
  274.   else
  275.     err_table[state] = 0;
  276. }
  277.  
  278.  
  279.  
  280. /* turn off the shift recorded for the specified token in the specified state.
  281. Used when we resolve a shift-reduce conflict in favor of the reduction.  */
  282.  
  283. void
  284. flush_shift(state, token)
  285. int state;
  286. int token;
  287. {
  288.   register shifts *shiftp;
  289.   register int k, i;
  290. /*  register unsigned symbol; JF unused */
  291.  
  292.   shiftp = shift_table[state];
  293.  
  294.   if (shiftp)
  295.     {
  296.       k = shiftp->nshifts;
  297.       for (i = 0; i < k; i++)
  298.     {
  299.       if (shiftp->shifts[i] && token == accessing_symbol[shiftp->shifts[i]])
  300.         (shiftp->shifts[i]) = 0;
  301.     }
  302.     }
  303. }
  304.  
  305.  
  306. void
  307. log_resolution(state, LAno, token, resolution)
  308. int state, LAno, token;
  309. char *resolution;
  310. {
  311.   fprintf(foutput,
  312.       "Conflict in state %d between rule %d and token %s resolved as %s.\n",
  313.       state, LAruleno[LAno], tags[token], resolution);
  314. }
  315.  
  316.  
  317. void
  318. conflict_log()
  319. {
  320.   register int i;
  321.  
  322.   src_total = 0;
  323.   rrc_total = 0;
  324.  
  325.   for (i = 0; i < nstates; i++)
  326.     {
  327.       if (conflicts[i])
  328.     {
  329.       count_sr_conflicts(i);
  330.       count_rr_conflicts(i);
  331.       src_total += src_count;
  332.       rrc_total += rrc_count;
  333.     }
  334.     }
  335.  
  336.   total_conflicts();
  337. }
  338.   
  339.  
  340. void
  341. verbose_conflict_log()
  342. {
  343.   register int i;
  344.  
  345.   src_total = 0;
  346.   rrc_total = 0;
  347.  
  348.   for (i = 0; i < nstates; i++)
  349.     {
  350.       if (conflicts[i])
  351.     {
  352.       count_sr_conflicts(i);
  353.       count_rr_conflicts(i);
  354.       src_total += src_count;
  355.       rrc_total += rrc_count;
  356.  
  357.       fprintf(foutput, "State %d contains", i);
  358.  
  359.       if (src_count == 1)
  360.         fprintf(foutput, " 1 shift/reduce conflict");
  361.       else if (src_count > 1)
  362.         fprintf(foutput, " %d shift/reduce conflicts", src_count);
  363.  
  364.       if (src_count > 0 && rrc_count > 0)
  365.         fprintf(foutput, " and");
  366.  
  367.       if (rrc_count == 1)
  368.         fprintf(foutput, " 1 reduce/reduce conflict");
  369.       else if (rrc_count > 1)
  370.         fprintf(foutput, " %d reduce/reduce conflicts", rrc_count);
  371.  
  372.       putc('.', foutput);
  373.       putc('\n', foutput);
  374.     }
  375.     }
  376.  
  377.   total_conflicts();
  378. }
  379.  
  380.  
  381. void
  382. total_conflicts()
  383. {
  384.   extern int fixed_outfiles;
  385.  
  386.   if (src_total == expected_conflicts && rrc_total == 0)
  387.     return;
  388.  
  389.   if (fixed_outfiles)
  390.     {
  391.       /* If invoked under the name `yacc', use the output format
  392.      specified by POSIX.  */
  393.       fprintf(stderr, "conflicts: ", infile);
  394.       if (src_total > 0)
  395.     fprintf(stderr, " %d shift/reduce", src_total);
  396.       if (src_total > 0 && rrc_total > 0)
  397.     fprintf(stderr, ",");
  398.       if (rrc_total > 0)
  399.     fprintf(stderr, " %d reduce/reduce", rrc_total);
  400.       putc('\n', stderr);
  401.     }
  402.   else
  403.     {
  404.       fprintf(stderr, "%s contains", infile);
  405.  
  406.       if (src_total == 1)
  407.     fprintf(stderr, " 1 shift/reduce conflict");
  408.       else if (src_total > 1)
  409.     fprintf(stderr, " %d shift/reduce conflicts", src_total);
  410.  
  411.       if (src_total > 0 && rrc_total > 0)
  412.     fprintf(stderr, " and");
  413.  
  414.       if (rrc_total == 1)
  415.     fprintf(stderr, " 1 reduce/reduce conflict");
  416.       else if (rrc_total > 1)
  417.     fprintf(stderr, " %d reduce/reduce conflicts", rrc_total);
  418.  
  419.       putc('.', stderr);
  420.       putc('\n', stderr);
  421.     }
  422. }
  423.  
  424.  
  425. void
  426. count_sr_conflicts(state)
  427. int state;
  428. {
  429.   register int i;
  430.   register int k;
  431.   register int mask;
  432.   register shifts *shiftp;
  433.   register unsigned *fp1;
  434.   register unsigned *fp2;
  435.   register unsigned *fp3;
  436.   register int symbol;
  437.  
  438.   src_count = 0;
  439.  
  440.   shiftp = shift_table[state];
  441.   if (!shiftp) return;
  442.  
  443.   for (i = 0; i < tokensetsize; i++)
  444.     {
  445.       shiftset[i] = 0;
  446.       lookaheadset[i] = 0;
  447.     }
  448.  
  449.   k = shiftp->nshifts;
  450.   for (i = 0; i < k; i++)
  451.     {
  452.       if (! shiftp->shifts[i]) continue;
  453.       symbol = accessing_symbol[shiftp->shifts[i]];
  454.       if (ISVAR(symbol)) break;
  455.       SETBIT(shiftset, symbol);
  456.     }
  457.  
  458.   k = lookaheads[state + 1];
  459.   fp3 = lookaheadset + tokensetsize;
  460.  
  461.   for (i = lookaheads[state]; i < k; i++)
  462.     {
  463.       fp1 = LA + i * tokensetsize;
  464.       fp2 = lookaheadset;
  465.  
  466.       while (fp2 < fp3)
  467.     *fp2++ |= *fp1++;
  468.     }
  469.  
  470.   fp1 = shiftset;
  471.   fp2 = lookaheadset;
  472.  
  473.   while (fp2 < fp3)
  474.     *fp2++ &= *fp1++;
  475.  
  476.   mask = 1;
  477.   fp2 = lookaheadset;
  478.   for (i = 0; i < ntokens; i++)
  479.     {
  480.       if (mask & *fp2)
  481.     src_count++;
  482.  
  483.       mask <<= 1;
  484.       if (mask == 0)
  485.     {
  486.       mask = 1;
  487.       fp2++;
  488.     }
  489.     }
  490. }
  491.  
  492.  
  493. void
  494. count_rr_conflicts(state)
  495. int state;
  496. {
  497.   register int i;
  498.   register int j;
  499.   register int count;
  500.   register unsigned mask;
  501.   register unsigned *baseword;
  502.   register unsigned *wordp;
  503.   register int m;
  504.   register int n;
  505.  
  506.   rrc_count = 0;
  507.  
  508.   m = lookaheads[state];
  509.   n = lookaheads[state + 1];
  510.  
  511.   if (n - m < 2) return;
  512.  
  513.   mask = 1;
  514.   baseword = LA + m * tokensetsize;
  515.   for (i = 0; i < ntokens; i++)
  516.     {
  517.       wordp = baseword;
  518.  
  519.       count = 0;
  520.       for (j = m; j < n; j++)
  521.     {
  522.       if (mask & *wordp)
  523.         count++;
  524.  
  525.       wordp += tokensetsize;
  526.     }
  527.  
  528.       if (count >= 2) rrc_count++;
  529.  
  530.       mask <<= 1;
  531.       if (mask == 0)
  532.     {
  533.       mask = 1;
  534.       baseword++;
  535.     }
  536.     }
  537. }
  538.  
  539.  
  540. void
  541. print_reductions(state)
  542. int state;
  543. {
  544.   register int i;
  545.   register int j;
  546.   register int k;
  547.   register unsigned *fp1;
  548.   register unsigned *fp2;
  549.   register unsigned *fp3;
  550.   register unsigned *fp4;
  551.   register int rule;
  552.   register int symbol;
  553.   register unsigned mask;
  554.   register int m;
  555.   register int n;
  556.   register int default_LA;
  557.   register int default_rule;
  558.   register int cmax;
  559.   register int count;
  560.   register shifts *shiftp;
  561.   register errs *errp;
  562.   int nodefault = 0;
  563.  
  564.   for (i = 0; i < tokensetsize; i++)
  565.     shiftset[i] = 0;
  566.  
  567.   shiftp = shift_table[state];
  568.   if (shiftp)
  569.     {
  570.       k = shiftp->nshifts;
  571.       for (i = 0; i < k; i++)
  572.     {
  573.       if (! shiftp->shifts[i]) continue;
  574.       symbol = accessing_symbol[shiftp->shifts[i]];
  575.       if (ISVAR(symbol)) break;
  576.       /* if this state has a shift for the error token,
  577.          don't use a default rule.  */
  578.       if (symbol == error_token_number) nodefault = 1;
  579.       SETBIT(shiftset, symbol);
  580.     }
  581.     }
  582.  
  583.   errp = err_table[state];
  584.   if (errp)
  585.     {
  586.       k = errp->nerrs;
  587.       for (i = 0; i < k; i++)
  588.     {
  589.       if (! errp->errs[i]) continue;
  590.       symbol = errp->errs[i];
  591.       SETBIT(shiftset, symbol);
  592.     }
  593.     }
  594.  
  595.   m = lookaheads[state];
  596.   n = lookaheads[state + 1];
  597.  
  598.   if (n - m == 1 && ! nodefault)
  599.     {
  600.       default_rule = LAruleno[m];
  601.  
  602.       fp1 = LA + m * tokensetsize;
  603.       fp2 = shiftset;
  604.       fp3 = lookaheadset;
  605.       fp4 = lookaheadset + tokensetsize;
  606.  
  607.       while (fp3 < fp4)
  608.     *fp3++ = *fp1++ & *fp2++;
  609.  
  610.       mask = 1;
  611.       fp3 = lookaheadset;
  612.  
  613.       for (i = 0; i < ntokens; i++)
  614.     {
  615.       if (mask & *fp3)
  616.         fprintf(foutput, "    %-4s\t[reduce  %d  (%s)]\n",
  617.             tags[i], default_rule, tags[rlhs[default_rule]]);
  618.  
  619.       mask <<= 1;
  620.       if (mask == 0)
  621.         {
  622.           mask = 1;
  623.           fp3++;
  624.         }
  625.     }
  626.  
  627.       fprintf(foutput, "    $default\treduce  %d  (%s)\n\n",
  628.           default_rule, tags[rlhs[default_rule]]);
  629.     }
  630.   else if (n - m >= 1)
  631.     {
  632.       cmax = 0;
  633.       default_LA = -1;
  634.       fp4 = lookaheadset + tokensetsize;
  635.  
  636.       if (! nodefault)
  637.     for (i = m; i < n; i++)
  638.       {
  639.         fp1 = LA + i * tokensetsize;
  640.         fp2 = shiftset;
  641.         fp3 = lookaheadset;
  642.   
  643.         while (fp3 < fp4)
  644.           *fp3++ = *fp1++ & ( ~ (*fp2++));
  645.   
  646.         count = 0;
  647.         mask = 1;
  648.         fp3 = lookaheadset;
  649.         for (j = 0; j < ntokens; j++)
  650.           {
  651.         if (mask & *fp3)
  652.           count++;
  653.   
  654.         mask <<= 1;
  655.         if (mask == 0)
  656.           {
  657.             mask = 1;
  658.             fp3++;
  659.           }
  660.           }
  661.   
  662.         if (count > cmax)
  663.           {
  664.         cmax = count;
  665.         default_LA = i;
  666.         default_rule = LAruleno[i];
  667.           }
  668.   
  669.         fp2 = shiftset;
  670.         fp3 = lookaheadset;
  671.   
  672.         while (fp3 < fp4)
  673.           *fp2++ |= *fp3++;
  674.       }
  675.  
  676.       for (i = 0; i < tokensetsize; i++)
  677.         shiftset[i] = 0;
  678.  
  679.       if (shiftp)
  680.         {
  681.           k = shiftp->nshifts;
  682.           for (i = 0; i < k; i++)
  683.         {
  684.           if (! shiftp->shifts[i]) continue;
  685.           symbol = accessing_symbol[shiftp->shifts[i]];
  686.           if (ISVAR(symbol)) break;
  687.           SETBIT(shiftset, symbol);
  688.         }
  689.         }
  690.  
  691.       mask = 1;
  692.       fp1 = LA + m * tokensetsize;
  693.       fp2 = shiftset;
  694.       for (i = 0; i < ntokens; i++)
  695.     {
  696.       int defaulted = 0;
  697.  
  698.       if (mask & *fp2)
  699.         count = 1;
  700.       else
  701.         count = 0;
  702.  
  703.       fp3 = fp1;
  704.       for (j = m; j < n; j++)
  705.         {
  706.           if (mask & *fp3)
  707.         {
  708.           if (count == 0)
  709.             {
  710.               if (j != default_LA)
  711.             {
  712.               rule = LAruleno[j];
  713.               fprintf(foutput, "    %-4s\treduce  %d  (%s)\n",
  714.                   tags[i], rule, tags[rlhs[rule]]);
  715.             }
  716.               else defaulted = 1;
  717.  
  718.               count++;
  719.             }
  720.           else
  721.             {
  722.               if (defaulted)
  723.             {
  724.               rule = LAruleno[default_LA];
  725.               fprintf(foutput, "    %-4s\treduce  %d  (%s)\n",
  726.                   tags[i], rule, tags[rlhs[rule]]);
  727.               defaulted = 0;
  728.             }
  729.               rule = LAruleno[j];
  730.               fprintf(foutput, "    %-4s\t[reduce  %d  (%s)]\n",
  731.                   tags[i], rule, tags[rlhs[rule]]);
  732.             }
  733.         }
  734.  
  735.           fp3 += tokensetsize;
  736.         }
  737.  
  738.       mask <<= 1;
  739.       if (mask == 0)
  740.         {
  741.           mask = 1;
  742.           fp1++;
  743.         }
  744.     }
  745.  
  746.       if (default_LA >= 0)
  747.     {
  748.       fprintf(foutput, "    $default\treduce  %d  (%s)\n",
  749.           default_rule, tags[rlhs[default_rule]]);
  750.     }
  751.  
  752.       putc('\n', foutput);
  753.     }
  754. }
  755.  
  756.  
  757. void
  758. finalize_conflicts()
  759. {
  760.   FREE(conflicts);
  761.   FREE(shiftset);
  762.   FREE(lookaheadset);
  763. }
  764.